package com.example.myapplication.dialog;

import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import com.example.myapplication.ResourceTable;

public class NewAccountDialog {
    private CommonDialog dialog;

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public NewAccountDialog(Context context) {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_new_account, null, false);
        layout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.hide();
                }
            }
        });
        dialog = (CommonDialog) new CommonDialog(context)
                .setContentCustomComponent(layout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT
                        , DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setDuration(Integer.MAX_VALUE)
                .setAlignment(LayoutAlignment.CENTER)
                .setTransparent(true);
    }

    public void hide() {
        if (dialog != null && dialog.isShowing()) {
            dialog.hide();
        }
    }

    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }
}
